$("#message").html('');
function errmsg(text){
	$("#message").html('');
	$("#message").html('<span class="errmsg">'+text+'</span>');
	setTimeout(function(){$("#message").html('');}, 3000);
}
function okmsg(text){
	$("#message").html('');
	$("#message").html('<span class="okmsg">'+text+'</span>');
	setTimeout(function(){$("#message").html('');}, 3000);
}
var getDistrist = function(){
	$('#provinceid').change(function(){
		var provinceid = $(this).val();
		$.post(url+control+'/getDistrict',{provinceid:provinceid},function(data){
			 $(".loadDistrist").html(data); 
		});		
		return false;
	});
}
var add = function(){
	$('#adds').click(function(){
		$.post(url+control+'/'+action,{},function(data){
			 $("#dialog").html(data); 
		});	
		dialogs('Thêm mới '+title,width,'add');
		$('#dialog').dialog('open');	
		$(".ui-dialog-buttonset").append('<div class="fleft message"></div>');
		return false;
	});
}
var edit = function(){
	$('.edit').each(function(e){
		$(this).click(function(){
			dialogs('Sửa '+title,width,'edit');
			var id = $(this).attr('id');
			$.post(url+control+'/'+action,{id:id},function(data){
				 $("#dialog").html(data); 
			});	
			$('#dialog').dialog('open');	
			return false;
		});
	});	
}
var deletes = function(){
	$('.delete').each(function(e){
		$(this).click(function(){
			var id = $(this).attr('id');
			cdialog('dialog',url,control,id);
			$('#dialog').html('<br>Bạn có chắc chắn xoá không?');
			$('#dialog').dialog('open');				
			return false;
		});
	});
} 
var searchs = function(){
	$('#search').click(function(){
		getList(querySearch(),0);
	});
}
var deleteAll = function(){
	$("#delete").click(function(){
		 cdialog('dialog',url,control);
		 $('#dialog').html('<br>Bạn có chắc chắn xoá không?');
		 $('#dialog').dialog('open');	
		 return false;
	});
}
function querySearch(){
	var str_json = "";
	var name, val;
	$(".search").each(function(){
		name = $(this).attr('id');
		val = $(this).val();
		str_json+=',"'+name+'":"'+val+'"';
	});
	var str_json = "{" + str_json.substring(1) + "}";	
	return str_json;
}
function queryInsert(){
	var str = "";
	var name, val;
	$(".insert").each(function(){
		name = $(this).attr('id');
		val = $(this).val();
		str+=',"'+name+'":"'+val+'"';
	});
	var str = "{" + str.substring(1) + "}";
	return str;
}
function checkall(){
	$("#checkAll").click(function(){
		$(":checkbox[name='keys[]']").attr('checked', $('#checkAll').is(':checked'));   
	});
	$(":checkbox[name='keys[]']").click(function(){
			var len = $(":checkbox[name='keys[]']:checked").length;
			var len2 = $(":checkbox[name='keys[]']").length;
			$ch = (len==len2)?true:false;
			$("#checkAll").attr('checked',$ch);
	});
}
function cdialog(id,url,control,item,str_json){
	$('#'+id).dialog({
					autoOpen: false,
					width: 300,
					height:120,
					modal: true,
					title:'Xoá thông tin',
					buttons: {
						"Xoá": function() {
							var str = '';
							$(":checkbox[name='keys[]']").each(function(e){
								if($(this).is(':checked')){
									str+= ','+$(this).attr('id');
								}
							});
							var str = str.substring(1);
							$.post(url+control+'/delete',{str:str,item:item},function(data){
								getList(str_json);	
							});
							$(this).dialog("close");
							return false;
						},
						"Huỷ": function() {
							$(this).dialog("close");
						}
					}
				});
}
function masager(id,url,control,item){
	$('#'+id).dialog({
					autoOpen: false,
					width: 300,
					height:120,
					modal: true,
					title:'Thông báo',
					buttons: {
						"Đóng": function() {
							$(this).dialog("close");
						}
					}
				});
}
function paging(getList,page){
	var end = '';
	var str_json = jsonSearch(end);
	$("#paging a").each(function(){
		$(this).click(function(){
			cpage = $(this).attr('name');
			getList(str_json,cpage);
			return false;
		});
	});
}
function mgstcloi(txt){
	$.msgBox({
			title: "Thông báo",
			type: "error",
			content: txt,
			buttons: [{value: 'OK'}]
		});
}
function mgstc(txt){
	$.msgBox({
			title: "Thông báo",
			content: txt,
			buttons: [{value: 'OK'}]
		});
}
function jsonSearch(end){
	var str_json = "";
	var name, val;
	$(".search").each(function(){
		name = $(this).attr('id');
		val = $(this).val();
		str_json+=',"'+name+'":"'+val+'"';
	});
	str_json = "{" + str_json.substring(1) + end + "}";
	return str_json;
}

function jsonSearch(end){
	var str_json = "";
	var name, val;
	$(".search").each(function(){
		name = $(this).attr('id');
		val = $(this).val();
		str_json+=',"'+name+'":"'+val+'"';
	});
	str_json = "{" + str_json.substring(1) + end + "}";
	return str_json;
}

jQuery.extend({
	random: function(X) {
	    return Math.floor(X * (Math.random() % 1));
	},
	randomBetween: function(MinV, MaxV) {
	  return MinV + jQuery.random(MaxV - MinV + 1);
	}
});

(function(jQuery)
{
  jQuery.fn.clock = function(options)
  {
    var defaults = {
	  year:2011, 
	  month:11, 
	  date:11, 
	  hour:11, 
	  minute:19, 
	  second:23,
      numday:30
    };
    var _this = this;
    var opts = jQuery.extend(defaults, options);

    setInterval( function() {
      
  		opts.second++;
		if(opts.second > 59 )
		{
			opts.second = 0;
			opts.minute++;
		}
		
		if(opts.minute > 59 )
		{
			opts.minute = 0;
			opts.hour++;
		}
		
		if(opts.hour > 23 )
		{
			opts.hour = 0;
			opts.date++;
		}
		
		if(opts.date > opts.numday)
		{
			opts.date = 1;
			opts.month++;
		}
		
		if(opts.month > 12 )
		{
			opts.month = 1;
			opts.year++;
		}
		
		
			
		second = opts.second;
		if(second < 10  )
			second = "0"+second;

		minute = opts.minute;
		if(minute < 10  )
			minute = "0"+minute;
			
		hour = 	opts.hour;
		if(hour < 10  )
			hour = "0"+hour;
		date = opts.date;
		if(date < 10  )
			date = "0"+date;

		month = opts.month;
		if(month < 10  )
			month = "0"+month;
			
		var str = hour+':'+minute+':'+second+' '+date+'-'+month+'-'+opts.year;
        jQuery(_this).html(str);
    }, 1000 );

  }
})(jQuery);
